package com.authine.cloudpivot.ext.domain.jizhong.vo;

import com.bssc.supplier.sdk.order.detail.BsscOrderDetailResp;
import com.bssc.supplier.sdk.order.detail.entity.BsscOrderGoodsEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Peter
 * @version 1.0.0
 * @date 2021/4/8 14:08
 * @desc 调用固买下单接口订单Vo
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class BsscOrderVo {

    /**
     * 外部用户ID
     */
    private String clientUserId;
    /**
     * 外部客户
     */
    private String clientId;
    /**
     * 外部订单ID
     */
    private String clientOrderId;
    /**
     * ERP PO
     */
    private String erpPO;
    /**
     * 税后商品运费共计
     */
    private Double amount;
    /**
     * 收货人
     */
    private String receiver;
    /**
     * 电话
     */
    private String phone;
    /**
     * 地址
     */
    private String address;
    /**
     * 订单商品VO
     */
    private List<BsscOrderGoodsVo> orderGoodsVo;
    /**
     * OS订单号
     */
    private String clientOs;
    /**
     * 实际下单人
     */
    private String actualOrder;
    /**
     * 外部工厂代码
     */
    private String clientFactoryCode;

    /**
     * 查询冀中订单详情
     *
     * @param orderDetail 冀中订单详情
     * @return 固买下单Vo
     */
    public static BsscOrderVo bsscOrderDetailRespToBsscOrderVo(BsscOrderDetailResp orderDetail) {

        // 固买订单ID
        String mallOrderNo = orderDetail.getResult().getMallOrderNo();
        // 冀中电商订单ID
        String orderNo = orderDetail.getResult().getOrderNo();
        // 订单金额
        Double orderAmount = orderDetail.getResult().getOrderAmount().doubleValue();
        // 下单人手机号
        String mobile = orderDetail.getResult().getMobile();
        // 下单人名称
        String buyer = orderDetail.getResult().getBuyer();
        // 详细地址
        String address = orderDetail.getResult().getAddress();

        // 冀中商品集合
        List<BsscOrderGoodsEntity> orderGoods = orderDetail.getResult().getOrderGoods();
        List<BsscOrderGoodsVo> bsscOrderGoodsVoList = new ArrayList<>();

        for (BsscOrderGoodsEntity orders : orderGoods) {
            BsscOrderGoodsVo bsscOrderGoodsVo = BsscOrderGoodsVo.builder().goodsId(Long.valueOf(orders.getGoodsId())).src(4).clientOrderGoodsId(orderNo + "||" + orders.getGoodsId()).num(orders.getNum().doubleValue()).build();
            bsscOrderGoodsVoList.add(bsscOrderGoodsVo);
        }
        // 此处传的 clientID没用 订单接口会重新获取session里的clientID
        return BsscOrderVo.builder().clientUserId("jzny").clientId("KFZEMwpIRg5").clientOrderId(orderNo).amount(orderAmount).receiver(buyer).phone(mobile).address(address).orderGoodsVo(bsscOrderGoodsVoList).build();
    }

    /**
     * 测试
     *
     * @return
     */
    public static BsscOrderVo test() {

        BsscOrderGoodsVo build1 = BsscOrderGoodsVo.builder().goodsId(322854872446472192L).src(4).clientOrderGoodsId("123123").num(20.0).build();
        BsscOrderGoodsVo build2 = BsscOrderGoodsVo.builder().goodsId(222459277807378554L).src(4).clientOrderGoodsId("234543").num(20.0).build();
        return BsscOrderVo.builder().clientUserId("001").clientOrderId("000").erpPO("002").amount(20.0).receiver("").phone("13145667468").address("上海市").orderGoodsVo(Arrays.asList(build1, build2)).build();
    }
}
